#include <iostream>
#include <queue>
#include <map>
//#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
//#include <unordered_set>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
priority_queue<ll,vector<ll> ,greater<ll>> q;
int main()
{
	int n;
	cin>>n;
	ll sum=0;
    for(int i=0;i<n;i++)
    {
    	int t;
    	cin>>t;
    	q.push(t);
	}
	ll ans=0;
	if(q.size()==1)
	{
		cout<<q.top();
		return 0;
	}
	while(q.size()>1)
	{
		ll a=q.top();
		q.pop();
		ll b=q.top();
		q.pop();
		ll sum=a+b;
		ans=ans+sum;
		
		if(q.size()==0) break;
		q.push(sum);
	}
	cout<<ans;

	return 0;
}

